
package thes2sim;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Logging {

   private String log1;
   private String log2;
   private String log3;
   private String log4;
   private String log5;
   private String log6;
   private String log7;
   private String log8;
   
   private int type;
   private int type2;
   private boolean append_to_file = true;
   
   public Logging (int type) {
       
       this.type = type;
       initLogFiles();
   }
   
   public void initLogFiles() {
        
        
        switch (type) {
           
           // Original Algo
           case 0: 
               log1 = "Detailed Activity Log - Original Algo.csv";
               log2 = "Inserted Frame Log - Original Algo.csv";
               log3 = "Discarded Frame Log - Original Algo.csv";
               log4 = "Transmitted Frame Log - Original Algo.csv";
               log5 = "Transmit Queue Content - Original Algo.csv";
               type2 = 1;
               break;
              
           // Fair Queuing Algo
           case 1: 
               log1 = "Detailed Activity Log - Fair Queuing Algo.csv";
               log2 = "Inserted Frame Log - Fair Queuing Algo.csv";
               log3 = "Discarded Frame Log - Fair Queuing Algo.csv";
               log4 = "Transmitted Frame Log - Fair Queuing Algo.csv";
               // skip log 5
               log6 = "High Queue Content - Fair Queuing Algo.csv";
               log7 = "Mid Queue Content - Fair Queuing Algo.csv";
               log8 = "Low Queue Content - Fair Queuing Algo.csv";
               type2 = 2;
               break;
               
           // Aging Queuing Algo    
           case 2: 
               log1 = "Detailed Activity Log - Aging Queuing Algo.csv";
               log2 = "Inserted Frame Log - Aging Queuing Algo.csv";
               log3 = "Discarded Frame Log - Aging Queuing Algo.csv";
               log4 = "Transmitted Frame Log - Aging Queuing Algo.csv";
               // skip log 5
               log6 = "High Queue Content - Aging Queuing Algo.csv";
               log7 = "Mid Queue Content - Aging Queuing Algo.csv";
               log8 = "Low Queue Content - Aging Queuing Algo.csv";
               type2 = 3;
               break;
               
               
           // Round Robin Queuing Algo    
           case 3: 
               log1 = "Detailed Activity Log - RRobin Queuing Algo.csv";
               log2 = "Inserted Frame Log - RRobin Queuing Algo.csv";
               log3 = "Discarded Frame Log - RRobin Queuing Algo.csv";
               log4 = "Transmitted Frame Log - RRobin Queuing Algo.csv";
               // skip log 5
               log6 = "High Queue Content - RRobin Queuing Algo.csv";
               log7 = "Mid Queue Content - RRobin Queuing Algo.csv";
               log8 = "Low Queue Content - RRobin Queuing Algo.csv";
               type2 = 2;
               break;

       }
       
       try {
           initHeaders();
       } catch (IOException ex) {
           Logger.getLogger(Logging.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
   
   public void initHeaders() throws IOException {
       
        FileWriter fw = null;
        BufferedWriter writer = null;
    
        String s1 = "\"=COUNTIF(B:B, 1)\""; // Total for High
        String s2 = "\"=COUNTIF(B:B, 2)\""; // Total for Mid
        String s3 = "\"=COUNTIF(B:B, 3)\""; // Total fow Low
        
        // Unique for Aging Algo
        String s4 = "\"=COUNTIF(E:E, 1)\"";
        String s5 = "\"=COUNTIF(E:E, 2)\"";
        String s6 = "\"=COUNTIF(E:E, 3)\"";
                 
        switch(type2) {
        
            case 1:
                writer = new BufferedWriter( new FileWriter(log1));
                writer.write("SimTime, Activity, id, Priority, Create Time");
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log2));
                writer.write("id, Priority, Create Time,"
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log3));
                writer.write("id, Priority, Create Time, Time Discarded," 
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log4));
                writer.write("id, Priority, Create Time, Time Exited,"
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log5));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();
                break;
                
            case 2:
                writer = new BufferedWriter( new FileWriter(log1));
                writer.write("SimTime, Activity, id, Priority, Create Time");
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log2));
                writer.write("id, Priority, Create Time,"
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log3));
                writer.write("id, Priority, Create Time, Time Discarded," 
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log4));
                writer.write("id, Priority, Create Time, Time Exited,"
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log6));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log7));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log8));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();

                break;
            
            // Unique for Aging Algo
            case 3:
                writer = new BufferedWriter( new FileWriter(log1));
                writer.write("SimTime, Activity, id, Priority, Create Time, Original Priority");
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log2));
                writer.write("id, Priority, Create Time,"
                            + s1 + ',' + s2 + ',' + s3);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log3));
                writer.write("id, Priority, Create Time, Time Discarded, Original Priority," 
                            + s4 + ',' + s5 + ',' + s6);
                writer.newLine();
                writer.flush();

                writer = new BufferedWriter( new FileWriter(log4));
                writer.write("id, Priority, Create Time, Time Exited, Original Priority,"
                            + s4 + ',' + s5 + ',' + s6);
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log6));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log7));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();
                
                writer = new BufferedWriter( new FileWriter(log8));
                writer.write("id, Priority");
                writer.newLine();
                writer.flush();

                break;
        
        }
   
   }
    
   /*
   1 - Detailed Activity Log
   2 - Inserted Frame Log
   3 - Discarded Frame Log
   4 - Transmitted Frame Log
   5 - Transmit Queue Content Log
   6 - High Queue Content Log
   7 - Mid Queue Content Log
   8 - Low Queue Content Log
   */
   
   public void logActivity(String txt, int type) throws IOException {
       
       FileWriter fw = null;
       
       switch (type){
           case 1: fw = new FileWriter(log1, append_to_file);
               break;
           case 2: fw = new FileWriter(log2, append_to_file);
               break;
           case 3: fw = new FileWriter(log3, append_to_file);    
               break;
           case 4: fw = new FileWriter(log4, append_to_file);
               break;
           case 5: fw = new FileWriter(log5, append_to_file);
               break;
           case 6: fw = new FileWriter(log6, append_to_file);
               break;
           case 7: fw = new FileWriter(log7, append_to_file);
               break;
           case 8: fw = new FileWriter(log8, append_to_file);
               break;    
       }
       
       PrintWriter pw = new PrintWriter(fw);
       pw.println(txt);
       pw.close();
   
   
   }
      
}
